package cn.amarone.scaffold.controller;

import cn.amarone.scaffold.config.business.common.BaseController;
import cn.amarone.scaffold.config.business.common.BaseReq;
import cn.amarone.scaffold.config.business.common.BaseReqPage;
import cn.amarone.scaffold.config.business.common.BaseRsp;
import cn.amarone.scaffold.entity.SysUser;
import cn.amarone.scaffold.entity.vo.SysUserVo;
import cn.amarone.scaffold.service.ISysUserService;
import com.alibaba.fastjson2.JSON;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static cn.amarone.scaffold.entity.table.SysUserTableDef.SYS_USER;

/**
 * @author Amarone
 * @version 1.0
 * @description: 用户管理
 * @date 2023/8/1 13:46
 */
@Tag(name = "用户管理")
@RestController
@ApiSupport(author = "amarone")
@RequestMapping("/sysUser")
@Slf4j
public class SysUserController extends BaseController {

    @Autowired
    protected ISysUserService sysUserService;

    @Override
    @GetMapping("/queryByPage")
    public BaseRsp<Page<SysUser>> queryByPage(@RequestBody BaseReqPage para) {
        log.info("请求参数: {} ", JSON.toJSONString(para));
        return BaseRsp.success(sysUserService.page(new Page<SysUser>(para.getPageNumber(), para.getPageSize()), QueryWrapper.create().from(SYS_USER).where(SYS_USER.AGE.ge(2))));
    }

    @Override
    @GetMapping("/detail")
    public BaseRsp<SysUserVo> queryDetail(@RequestBody BaseReq para) {
        return BaseRsp.success(sysUserService.queryChain()
                .select()
                .from(SYS_USER)
                .where(SYS_USER.ID.eq("43870324229349376"))
                .limit(1)
                .oneWithRelationsAs(SysUserVo.class)
        );
    }
}
